package com.xqb.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqb.common.constant.Constants;
import com.xqb.common.exception.ServiceException;
import com.xqb.common.utils.DateUtils;
import com.xqb.common.utils.SecurityUtils;
import com.xqb.modules.domain.po.LesCourseEvaluation;
import com.xqb.modules.domain.po.StdLikedRecord;
import com.xqb.modules.domain.vo.CourseEvaluationVo;
import com.xqb.modules.mapper.LesCourseEvaluationMapper;
import com.xqb.modules.mapper.StdLikedRecordMapper;
import com.xqb.modules.service.ILesCourseEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 2092466583
* @description 针对表【les_course_evaluation】的数据库操作Service实现
* @createDate 2024-10-16 10:32:23
*/
@Service
@Slf4j
public class LesCourseEvaluationServiceImpl extends ServiceImpl<LesCourseEvaluationMapper, LesCourseEvaluation>
    implements ILesCourseEvaluationService {
    @Autowired
    private LesCourseEvaluationMapper lesCourseEvaluationMapper;
    @Autowired
    private StdLikedRecordMapper stdLikedRecordMapper;

    /**
     * 查询课程评论
     *
     * @param id 课程评论主键
     * @return 课程评论
     */
    @Override
    public LesCourseEvaluation selectLesCourseEvaluationById(Long id)
    {
        return lesCourseEvaluationMapper.selectLesCourseEvaluationById(id);
    }

    /**
     * 查询课程评论列表
     *
     * @param lesCourseEvaluation 课程评论
     * @return 课程评论
     */
    @Override
    public List<LesCourseEvaluation> selectLesCourseEvaluationList(LesCourseEvaluation lesCourseEvaluation)
    {
        return lesCourseEvaluationMapper.selectLesCourseEvaluationList(lesCourseEvaluation);
    }

    /**
     * 新增课程评论
     *
     * @param lesCourseEvaluation 课程评论
     * @return 结果
     */
    @Override
    public int insertLesCourseEvaluation(LesCourseEvaluation lesCourseEvaluation)
    {
        lesCourseEvaluation.setCreateTime(DateUtils.getNowDate());
        if(lesCourseEvaluation.getUserId()==null){
            lesCourseEvaluation.setUserId(SecurityUtils.getUserId());
        }
        return lesCourseEvaluationMapper.insertLesCourseEvaluation(lesCourseEvaluation);
    }

    /**
     * 修改课程评论
     *
     * @param lesCourseEvaluation 课程评论
     * @return 结果
     */
    @Override
    public int updateLesCourseEvaluation(LesCourseEvaluation lesCourseEvaluation)
    {
        lesCourseEvaluation.setUpdateTime(DateUtils.getNowDate());
        return lesCourseEvaluationMapper.updateLesCourseEvaluation(lesCourseEvaluation);
    }

    /**
     * 批量删除课程评论
     *
     * @param ids 需要删除的课程评论主键
     * @return 结果
     */
    @Override
    public int deleteLesCourseEvaluationByIds(Long[] ids)
    {
        return lesCourseEvaluationMapper.deleteLesCourseEvaluationByIds(ids);
    }

    /**
     * 删除课程评论信息
     *
     * @param id 课程评论主键
     * @return 结果
     */
    @Override
    public int deleteLesCourseEvaluationById(Long id)
    {
        return lesCourseEvaluationMapper.deleteLesCourseEvaluationById(id);
    }

    /**
     * 查询课程详情评价列表
     *
     * @param lesCourseEvaluation 课程评价
     * @return 课程评价
     */
    public List<CourseEvaluationVo> getLesCourseEvaluationList(LesCourseEvaluation lesCourseEvaluation) {
        List<CourseEvaluationVo> courseEvaluationVos = new ArrayList<>();
        if (lesCourseEvaluation.getCourseId() == null) {
            return courseEvaluationVos;
        }
        courseEvaluationVos = lesCourseEvaluationMapper.getLesCourseEvaluationList(lesCourseEvaluation);
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId != null && courseEvaluationVos != null) {
                List<Long> evaluationIds = courseEvaluationVos.stream()
                        .map(CourseEvaluationVo::getId)
                        .collect(Collectors.toList());
                if (evaluationIds.isEmpty()) {
                    return courseEvaluationVos;
                }
                List<StdLikedRecord> likedRecords = stdLikedRecordMapper.selectList(
                        new LambdaQueryWrapper<StdLikedRecord>()
                                .eq(StdLikedRecord::getUserId, userId)
                                .in(StdLikedRecord::getBizId, evaluationIds)
                                .eq(StdLikedRecord::getBizType, Constants.COURSE_EVALUATION)
                );
                Map<Long, Boolean> likedMap = likedRecords.stream()
                        .collect(Collectors.toMap(StdLikedRecord::getBizId, record -> true, (existing, replacement) -> existing));
                for (CourseEvaluationVo vo : courseEvaluationVos) {
                    vo.setLiked(likedMap.getOrDefault(vo.getId(), false));
                }
            }
        } catch (ServiceException e) {
            for (CourseEvaluationVo vo : courseEvaluationVos) {
                vo.setLiked(false);
            }
        }

        return courseEvaluationVos;
    }

    /**
     * 点赞或取消点赞
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLikes(Long id) {
        boolean res = false;
        LambdaQueryWrapper<StdLikedRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LesCourseEvaluation lesCourseEvaluation = lesCourseEvaluationMapper.selectLesCourseEvaluationById(id);
        lambdaQueryWrapper.eq(StdLikedRecord::getUserId, SecurityUtils.getUserId())
                .eq(StdLikedRecord::getBizId, id)
                .eq(StdLikedRecord::getBizType, Constants.COURSE_EVALUATION);
        StdLikedRecord selectStdLikedRecord = stdLikedRecordMapper.selectOne(lambdaQueryWrapper);
        if (selectStdLikedRecord != null) {
            stdLikedRecordMapper.deleteById(selectStdLikedRecord.getId());
            lesCourseEvaluation.setSupportNum(lesCourseEvaluation.getSupportNum() - 1);
        } else {
            StdLikedRecord stdLikedRecord = new StdLikedRecord();
            stdLikedRecord.setUserId(SecurityUtils.getUserId());
            stdLikedRecord.setBizId(id);
            stdLikedRecord.setBizType(Constants.COURSE_EVALUATION);
            log.info("新增点赞记录：{}", stdLikedRecord);
            stdLikedRecordMapper.insert(stdLikedRecord);
            lesCourseEvaluation.setSupportNum(lesCourseEvaluation.getSupportNum() + 1);
            res = true;
        }
        lesCourseEvaluationMapper.updateById(lesCourseEvaluation);
        return res;
    }
}




